﻿#region using
using AwesomeTanks.Sources.GameElements.ShootElements;
using AwesomeTanks.Sources.GameElements.TankElements;
using Microsoft.Xna.Framework;
using AwesomeTanks.Sources.GameElements.MapElements;
using AwesomeTanks.Sources.Shape;
using AwesomeTanks.Sources.GameElements;
using AwesomeTanks.Sources.Screens;
using AwesomeTanks.Sources.Tools;

#endregion

namespace AwesomeTanks.Sources.Collision
{
    /// <summary>
    /// 
    /// </summary>
    public class ShootCollisions
    {
        #region Fields
        private static int SHOOTHURTSCORE = 100;
        private static int SHOOTDESTROYSCORE = 1000;
        #endregion

        #region Methods
        /// <summary>
        /// Tanks the collision.
        /// </summary>
        /// <param name="shoot">The shoot.</param>
        /// <param name="tank">The tank.</param>
        /// <param name="gameTime">The game time.</param>
        public static void TankCollision(Shoot shoot, Tank tank, GameTime gameTime)
        {
            foreach (Circle circleShoot in shoot.HitboxList)
            {
                foreach (Circle circleTank in tank.HitboxList)
                {
                    if (circleShoot.IntersectsCircle(circleTank))
                    {
                        if (shoot.Shooter.Tank != tank)
                        {
                            if (shoot.Shooter.TeamColor == Player.getTeamByTank(tank) && OptionsMenuScreen.FriendlyFire)
                            {
                                shoot.ToRemove = true;
                                if (shoot.AlreadyHit)
                                {
                                    if (shoot.ShootType == ShootType.ROCKET)
                                    {
                                        tank.CurrentLife -= tank.MaxLife / 2;
                                        shoot.RocketHit();
                                    }
                                    else
                                    {
                                        tank.CurrentLife -= shoot.Damages / 2;
                                        shoot.Hit();
                                    }

                                }
                                else
                                {
                                    tank.CurrentLife -= shoot.Damages;
                                    if (shoot.ShootType == ShootType.ROCKET)
                                    {
                                        shoot.RocketHit();
                                    }
                                    else
                                    {
                                        shoot.Hit();
                                    }

                                }

                                shoot.Shooter.Score -= SHOOTHURTSCORE;
                                if (tank.CurrentLife <= 0)
                                {
                                    shoot.Shooter.Score -= SHOOTDESTROYSCORE;
                                    tank.Explode();
                                }
                            }
                            else if (shoot.Shooter.TeamColor != Player.getTeamByTank(tank))
                            {
                                shoot.ToRemove = true;
                                if (shoot.AlreadyHit)
                                {
                                    if (shoot.ShootType == ShootType.ROCKET)
                                    {
                                        tank.CurrentLife -= tank.MaxLife / 2;
                                        shoot.RocketHit();
                                    }
                                    else
                                    {
                                        tank.CurrentLife -= shoot.Damages / 2;
                                        shoot.Hit();
                                    }


                                }
                                else
                                {
                                    tank.CurrentLife -= shoot.Damages;
                                    if (shoot.ShootType == ShootType.ROCKET)
                                    {
                                        shoot.RocketHit();
                                    }
                                    else
                                    {
                                        shoot.Hit();
                                    }
                                }

                                shoot.Shooter.Score += SHOOTHURTSCORE;
                                if (tank.CurrentLife <= 0)
                                {
                                    shoot.Shooter.Score += SHOOTDESTROYSCORE;
                                    tank.Explode();
                                }
                            }
                        }
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// Walls the collision.
        /// </summary>
        /// <param name="shoot">The shoot.</param>
        /// <param name="wall">The wall.</param>
        public static void WallCollision(Shoot shoot, Wall wall)
        {

            Rectangle r = new Rectangle((int)wall.Position.X, (int)wall.Position.Y, (int)wall.Scale.X, (int)wall.Scale.Y);
            foreach (Circle circle in shoot.HitboxList)
            {
                if (circle.Intersectsrectangle(r))

                    if (wall.Health == null)
                    {
                        if (!shoot.AlreadyHit && OptionsMenuScreen.BoucingShoot)
                        {
                            shoot.AlreadyHit = true;
                            if (wall.Scale.X > wall.Scale.Y)
                            {
                                shoot.Speed = new Vector2(shoot.Speed.X, -shoot.Speed.Y);
                                shoot.Rotation = shoot.Speed.ToAngle();
                            }
                            else
                            {
                                shoot.Speed = new Vector2(-shoot.Speed.X, shoot.Speed.Y);
                                shoot.Rotation = shoot.Speed.ToAngle();
                            }
                        }
                        else
                        {
                            shoot.ToRemove = true;
                            if (shoot.ShootType == ShootType.ROCKET)
                            {
                                shoot.RocketHit();
                            }
                            else
                            {
                                shoot.Hit();
                            }

                        }

                    }
                    else
                    {
                        if (shoot.ShootType == ShootType.ROCKET)
                        {
                            if (!shoot.AlreadyHit)
                            {
                                wall.Health = 0;
                                shoot.AlreadyHit = true;
                            }
                            else
                            {
                                wall.Health -= wall.MaxHealth / 2;
                                shoot.ToRemove = true;
                            }
                        }
                        else
                        {
                            shoot.ToRemove = true;
                            wall.Health -= shoot.Damages;
                            shoot.Hit();
                        }
                    }
            }
        }
        #endregion
    }
}